import {
	ref
} from 'vue'


import {
	defineStore
} from 'pinia'

import {
	Api
} from '@/js/api.js'

export const useUserStore = defineStore('user', () => {
	const user = ref({})
	const userInfo = ref({})
	const userStatistics = ref({})
	const isLogin = ref(false)
	const userLogo = ref('')

	const updateUserInfo = (cb) => {
		Api.get('/auth/info', {}, (data) => {
			isLogin.value = data.success
			if (data.success) {
				user.value = data.data.user
				userInfo.value = data.data.info
				userStatistics.value = data.data.statistics
				userLogo.value = Api.userlogo(user.value.id)
			} else {
				user.value = {}
				userInfo.value = {}
				userStatistics.value = {}
				userLogo.value = ''

			}
			cb(data.success)
		})
	}

	return {
		user,
		userInfo,
		userStatistics,
		isLogin,
		userLogo,
		updateUserInfo
	}
})